// pages/searchcity/searchcity.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      list: [],
      is_search: false,
      search_list: [],
      longitude: '',
      latitude: '',
      dingweiCity: null,
      current_city: null,
      history: [],
      keyword: ''
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (t) {
      getApp().page.onLoad(this, t);
      this.dingwei();
      var city = wx.getStorageSync('store_info');
      var history = wx.getStorageSync("store_select_history");
      this.setData({
        history: history ? history : [],
        current_city: city ? city : this.data.store
      })
    },
    onInput: function (t) {
      this.setData({
        keyword: t.detail.value
      });
    },
    onInputConfirm: function () {
      if (!this.data.keyword) {
        wx.showToast({
          title: '请输入关键词',
          icon: 'none'
        })
        return;
      }
      this.loadData('search');
    },
    onDeleteKeyword: function () {
      this.setData({
        search_list: [],
        keyword: ''
      })
    },
    // 定位最近城市
    dingwei: function () {
      var self = this;
      wx.getLocation({
        success: function (t) {
          self.setData({
            longitude: t.longitude,
            latitude: t.latitude
          });
          getApp().request({
            url: getApp().api.default.shortest_store,
            data: {
              longitude: t.longitude,
              latitude: t.latitude
            },
            success: function (res) {
              if (0 == res.code) {
                self.setData({
                  dingweiCity: res.data.store
                })
              }
            }
          })
        },
        complete: function () {
          self.loadData()
        }
      });
    },
    selectCity: function (e) {
      var id = e.currentTarget.dataset.id;
      var store = null;
      for (var i = 0; i < this.data.list.length; i++) {
        if (this.data.list[i].id == id) {
          store = this.data.list[i];
          break;
        }
      }
      if (store) {
        this.setHistory(store);
        wx.setStorageSync('store_info', {
          name: store.name,
          address: store.address,
          mobile: store.mobile,
          store_id: store.id
        });
        getApp().globalData.store_id = store.id;
        wx.setStorageSync("old_store_id", wx.getStorageSync("store_id") ? wx.getStorageSync("store_id") : 0);
        wx.setStorageSync("store_id", store.id);

        if ((wx.getStorageSync("store_id") != wx.getStorageSync("old_store_id")) && wx.getStorageSync("old_store_id") > 0) {
          getApp().core.setStorageSync(getApp().const.USER_INFO, {});
          getApp().core.removeStorageSync(getApp().const.ACCESS_TOKEN);
        }
        if (this.data.store.client_shop_select_module == 'all') {
          wx.reLaunch({
            url: '/mch/shop-region/shop-region',
          })
        } else {
          wx.reLaunch({
            url: "/pages/index/index",
          })
        }
      } else {
        wx.showToast({
          title: '所选城市不存在',
          icon: 'none'
        })
      }
    },
    setHistory: function (store) {
      var history = wx.getStorageSync("store_select_history");
      if (history) {
        var index = false;
        for (var i = 0; i < history.length; i++) {
          if (store.id == history[i].id) {
            index = i
            break;
          }
        }
        if (index !== false) {
          history.splice(index, 1);
        }
        history.unshift(store)
        if (history.length > 2) {
          history.splice(-1, 1);
        }
        wx.setStorageSync("store_select_history", history);
        this.setData({
          history: history
        })
      } else {
        var history = [];
        history.push(store);
        wx.setStorageSync("store_select_history", history);
        this.setData({
          history: history
        })
      }
    },
    onCancel: function () {
      if (this.data.store.client_shop_select_module == 'all') {
        wx.redirectTo({
          url: '/mch/shop-region/shop-region',
        })
      } else {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    },
    loadData: function(type) {
      var e = this;
      wx.showLoading({
          title: "加载中"
      }), getApp().request({
          url: getApp().api.default.store_list,
          method: "GET",
          data: {
              keyword: e.data.keyword,
              longitude: e.data.longitude,
              latitude: e.data.latitude
          },
          success: function(t) {
            if (t.code == 0) {
              if (type == 'search') {
                if (t.data.list.length == 0) {
                  wx.showToast({
                    title: '没有找到任何城市',
                    icon: 'none'
                  })
                } else {
                  e.setData({
                    search_list: t.data.list
                  })
                }
              } else {
                e.setData({
                  list: t.data.list
                })
              }
            }
          },
          fail: function(t) {},
          complete: function() {
              wx.hideLoading();
          }
      });
  },
  
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
  
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
  
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
  
    },
  
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
  
    }
  })